<?php
require_once 'function.php';
$page_no=isset($_POST['page_no'])?$_POST['page_no']:1;
//if (isset($_POST))die($_POST['content_id']);
$page_size=5;
if (isset($_GET['action']))$action=$_GET['action'];
if (isset($_POST['action']))$action=$_POST['action'];
if (isset($_POST['content_id']))$content_id=$_POST['content_id'];
function show_discuss_error($error_id){
	$error=new error_handle();
	$error_array=$error->get_error_info($error_id);
	if ($error_array!=FALSE){
		echo "
			<div class=\"discuss_ctn\">
				<div class=\"discuss_error_title_ctn\">
					<div class=\"discuss_info\">".$error_array[0]['title']."</div>
					<div class=\"discuss_func\"></div>
				</div>
				<div class=\"discuss_content_ctn\">".$error_array[0]['content'];
		echo "</div></div>";
	}
}
function display_discuss($discuss_array){
	global $discuss_back_array;
	foreach ($discuss_array as $val1){
		echo "
			<div class=\"discuss_ctn\">
				<div class=\"discuss_info_ctn\">
					<div class=\"discuss_info\"><a name=\"discuss_".$val1['id']."\"><span class=\"discuss_send_by\">".$val1['send_by']."</span></a> 于".$val1['send_time']."发表该帖</div>
					<div class=\"discuss_func\"><a href=\"javascript:reply_discuss('".$val1['id']."','".$val1['send_by']."')\">回复</a></div>
				</div>
				<div class=\"discuss_content_ctn\">".$val1['content'];
		if (isset($discuss_back_array[$val1['id']]))display_discuss($discuss_back_array[$val1['id']]);
		echo "</div></div>";
	}
}
function load_discuss($content_id,$page_no,$page_size){
	$discuss=new discuss();
	$discuss->get_discuss($content_id,$page_no,$page_size);
	$discuss_array=$discuss->discuss_idea;
	global $discuss_back_array;
	$discuss_back_array=$discuss->discuss_back;
	display_discuss($discuss_array);
	echo "<div id=\"discuss_page_link_ctn\">".page_link($discuss->discuss_count,$page_size,$page_no,"discuss_list_ctn","load_discuss")."</div>";
}
if ($action=="get_discuss"){
	load_discuss($content_id,$page_no,$page_size);
}
if ($action=="insert_discuss"){
	if ($_SESSION['authnum_session']==$_POST['authnum']){
		$discuss_content=htmlspecialchars(urldecode($_POST['discuss_content']));
		if (mb_strlen ( $discuss_content, 'UTF-8' )<=400){
			$father_id=$_POST['father_id']!=""?$_POST['father_id']:"";
			$send_by=$_POST['send_by']!=""?$_POST['send_by']:"";
			$topic_id=$_POST['content_id'];
			$discuss=new discuss();
			$discuss->update_discuss($discuss_content,"",$topic_id,$father_id,$send_by);
		}
		else {
			show_discuss_error('dscs_001');
		}
	}else 
		show_discuss_error('aun_001');
	load_discuss($content_id,$page_no,$page_size);
}
if ($action=="logout"){
	$login = new verify ();
	$login_state=$login->logout();
	echo "y";
}
?>